﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace lxwmvc
{
    class core_sms
    {
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <param name="content">内容</param>
        /// <returns>是否成功</returns>
        public static bool SendMsg(string mobile, string content)
        {
            try
            {

                content = content + "【嘉金金融】";

#if DEBUG
                //记录信息
                dataloger.info(mobile + "_" + content);
                return true;
#else
#endif

                Encoding dataEncode = Encoding.UTF8;
                //string paramData = "uid=103&username=jiajian&password=3D9188577CC9BFE9291AC66B5CC872B7&messageid=250&content=" + HttpUtility.UrlEncode(content, dataEncode) + "&mobiles=" + mobile + "&sendtime=";
                string paramData = "uid=103&username=jiajin&password=3D9188577CC9BFE9291AC66B5CC872B7&messageid=&sendtime=&content=" + content + "&mobiles=" + mobile ;
                byte[] byteArray = dataEncode.GetBytes(paramData); //转羇化ˉ
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri("http://v.369sms.com/SMS/SendSingleSMS"));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";
                webReq.ContentLength = byteArray.Length;
                webReq.Timeout = 5000;
                ServicePointManager.DefaultConnectionLimit = 100;
                System.IO.Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写′入?参?数簓
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
                if (ret.IndexOf("200")>-1)
                    return true;
                else
                    return false;
            }
            catch (Exception exp) 
            {
                dataloger.fatal("SendMsg", exp);
                return false; 
            }
        }

        

    }
}
